;; Copyright 2019 The Amber Authors.
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     https://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

;; Emacs mode for Amber script
;; https://github.com/google/amber

(defvar amber-mode-hook nil)

(defvar amber-mode-map
  (let ((map (make-keymap)))
    ;; Add key bindings here
    (define-key map "\C-j" 'newline-and-indent)
    map)
  "Keymap for Amber major mode")

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.amber\\'" . amber-mode))

;; Evaluate the following to create optimized regex used for highlighting

;; (regexp-opt '(
;;               "END" "SHADER" "BUFFER" "DATA_TYPE" "SIZE" "DATA" "FILL"
;;               "SERIES_FROM" "PASSTHROUGH" "PIPELINE" "ATTACH" "ENTRY_POINT"
;;               "DESCRIPTOR_SET" "INC_BY" "BINDING" "IDX" "TO" "RUN" "DRAW_RECT"
;;               "POS" "DRAW_ARRAY" "IN" "AS" "START_IDX" "COUNT" "CLEAR_COLOR"
;;               "CLEAR" "EXPECT" "TYPE" "FRAMEBUFFER" "SHADER_OPTIMIZATION"
;;               "FORMAT" "FRAMEBUFFER_SIZE" "BIND" "SAMPLER" "VERTEX_DATA"
;;               "INDEX_DATA" "INDEXED" "IMAGE_ATTACHMENT"
;;               "DEPTH_STENCIL_ATTACHMENT" "LOCATION" "DEVICE_FEATURE"
;;               "DERIVE_PIPELINE" "FROM" "COPY" "TOLERANCE" "REPEAT"
;;               ) t)

;; (regexp-opt '(
;;               "vertex" "fragment" "compute" "geometry" "tessellation_evaluation"
;;               "tessellation_control" "multi" "framebuffer" "graphics" "uniform"
;;               "storage" "push_constant" "color" "depth_stencil" "EQ" "NE" "LT"
;;               "LE" "GT" "GE" "EQ_RGB" "EQ_RGBA" "EQ_BUFFER" "GLSL" "HLSL"
;;               "SPIRV-ASM" "SPIRV-HEX" "OPENCL-C"
;;               ) t)

;; (regexp-opt '(
;;               "point_list" "line_list" "line_list_with_adjacency" "line_strip"
;;               "line_strip_with_adjacency" "triangle_list"
;;               "triangle_list_with_adjacency" "triangle_strip"
;;               "triangle_strip_with_adjacency" "triangle_fan" "patch_list"
;;               "float" "double"
;;               ) t)

(defconst amber-font-lock-keywords-1
  (list
   '("\\<\\(A\\(?:S\\|TTACH\\)\\|B\\(?:IND\\(?:ING\\)?\\|UFFER\\)\\|C\\(?:LEAR\\(?:_COLOR\\)?\\|O\\(?:PY\\|UNT\\)\\)\\|D\\(?:ATA\\(?:_TYPE\\)?\\|E\\(?:PTH_STENCIL_ATTACHMENT\\|RIVE_PIPELINE\\|SCRIPTOR_SET\\|VICE_FEATURE\\)\\|RAW_\\(?:ARRAY\\|RECT\\)\\)\\|E\\(?:N\\(?:D\\|TRY_POINT\\)\\|XPECT\\)\\|F\\(?:ILL\\|ORMAT\\|R\\(?:AMEBUFFER\\(?:_SIZE\\)?\\|OM\\)\\)\\|I\\(?:DX\\|MAGE_ATTACHMENT\\|N\\(?:C_BY\\|DEX\\(?:ED\\|_DATA\\)\\)?\\)\\|LOCATION\\|P\\(?:ASSTHROUGH\\|IPELINE\\|OS\\)\\|R\\(?:EPEAT\\|UN\\)\\|S\\(?:AMPLER\\|ERIES_FROM\\|HADER\\(?:_OPTIMIZATION\\)?\\|IZE\\|TART_IDX\\)\\|T\\(?:O\\(?:LERANCE\\)?\\|YPE\\)\\|VERTEX_DATA\\)\\>" . font-lock-builtin-face)
   '("\\<\\(EQ\\(?:_\\(?:BUFFER\\|RGBA?\\)\\)?\\|G\\(?:LSL\\|[ET]\\)\\|HLSL\\|L[ET]\\|NE\\|OPENCL-C\\|SPIRV-\\(?:ASM\\|HEX\\)\\|co\\(?:lor\\|mpute\\)\\|depth_stencil\\|fra\\(?:gment\\|mebuffer\\)\\|g\\(?:eometry\\|raphics\\)\\|multi\\|push_constant\\|storage\\|tessellation_\\(?:control\\|evaluation\\)\\|uniform\\|vertex\\)\\>" . font-lock-constant-face)
   '("\\<\\(double\\|float\\|line_\\(?:list\\(?:_with_adjacency\\)?\\|strip\\(?:_with_adjacency\\)?\\)\\|p\\(?:\\(?:atch\\|oint\\)_list\\)\\|triangle_\\(?:fan\\|list\\(?:_with_adjacency\\)?\\|strip\\(?:_with_adjacency\\)?\\)\\)\\>" . font-lock-type-face)
   ;; This one is hand-written, not generated by regexp-opt
   '("\\<\\(u?int\\|vec\\|mat\\)[[:digit:]]*\\>" . font-lock-type-face)
   )
  "Minimal highlighting expressions for Amber mode")

(defvar amber-font-lock-keywords amber-font-lock-keywords-1
  "Default highlighting expressions for Amber mode")

(defvar amber-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?_ "w" st)
    (modify-syntax-entry ?# "<" st)
    (modify-syntax-entry ?\n ">" st)
    (modify-syntax-entry ?\" "\"" st)
    st)
  "Syntax table for amber-mode")

(defun amber-mode ()
  "Major mode for editing Amber Script files"
  (interactive)
  (kill-all-local-variables)
  (set-syntax-table amber-mode-syntax-table)
  (use-local-map amber-mode-map)
  (set (make-local-variable 'font-lock-defaults) '(amber-font-lock-keywords))
  (setq major-mode 'amber-mode)
  (setq mode-name "amber")
  (run-hooks 'amber-mode-hook))

(provide 'amber-mode)
